package tech.aistar.controller.authordis;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import tech.aistar.entity.authordis.NoteAuthor;
import tech.aistar.entity.authordis.Notes;
import tech.aistar.entity.authordis.UserTable;
import tech.aistar.service.authordis.INoteTravelerDisService;
import tech.aistar.service.authordis.ITravelerInfoDisService;
import tech.aistar.service.noteinfo.INoteInfoService;

import javax.jws.WebParam;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.zip.Inflater;

/**
 * Created by lenovo on 2019/10/7.
 */
@Controller
@RequestMapping("/travelerInfoDis")
public class TravelerInfoDis {

    @Autowired
    private ITravelerInfoDisService travelerInfoDisService;

    @Autowired
    private INoteTravelerDisService noteTravelerDisService;

    @Autowired
    private INoteInfoService noteInfoService;

    @GetMapping("/trave")
    public String trav(){
        return "authordis/travelerinfo";
    }

    @RequestMapping("/travelerInfo")
    public String travelerInfo(Model model, HttpServletRequest req, HttpSession session){

        Integer id = Integer.valueOf(req.getParameter("id"));

        //通过用户id获取用户信息
        UserTable userTable = noteTravelerDisService.findUserById(id);
        //通过用户id获取用户游记作者页面信息
        NoteAuthor noteAuthor = travelerInfoDisService.findAuthorByUserId(id);

        model.addAttribute("userTable",userTable);
        model.addAttribute("noteAuthor",noteAuthor);

        return "authordis/travelerinfo";
    }

    @RequestMapping("/notesPage")
    public String notesPage(HttpSession session,Model model, Integer authorId, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "5") Integer pageSize){

        //获取已登录session作用域中的
        UserTable userLogin = null;
        userLogin = (UserTable) session.getAttribute("userId");
        Integer integer = null;
        if (userLogin != null) {
            integer = userLogin.getId();
        }
        //通过游记作者id获取游记作者信息
        NoteAuthor noteAuthor = noteInfoService.findNoteAuthorById(authorId);
        //获取游记作者的用户id
        Integer authorUserId = noteAuthor.getUserId();
        //如果是游记的作者查看游记个人中心,显示删除
        if (integer != null && integer == authorUserId) {
            model.addAttribute("del",1);
        }else{
            model.addAttribute("del",0);
        }

        PageInfo<Notes> pageInfo = travelerInfoDisService.findNotesByAuthorId(authorId,pageNum,pageSize);

        model.addAttribute("pageInfo",pageInfo);
        return "authordis/notesPageData";
    }

    @RequestMapping("/update")
    @ResponseBody
    public Boolean update(Integer id){
        travelerInfoDisService.updateNoteById(id);
        return true;
    }

}
